package com.ctl.szmd.szmd.feature_wm.ui.fragment

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import com.ctl.indicator.lib.commonnavigator.CommonNavigator
import com.ctl.indicator.lib.commonnavigator.abs.CommonNavigatorAdapter
import com.ctl.indicator.lib.commonnavigator.abs.IPagerIndicator
import com.ctl.indicator.lib.commonnavigator.abs.IPagerTitleView
import com.ctl.indicator.lib.commonnavigator.titles.CommonPagerTitleView
import com.ctl.indicator.lib.help.ViewPagerHelper
import com.ctl.lib_common.base.BaseConstants
import com.ctl.lib_common.base.ViewPagerFragmentStateAdapter
import com.ctl.lib_common.extensions.getColor
import com.ctl.lib_common.utils.LocalCache
import com.ctl.lib_common.utils.livebus.LiveBus
import com.ctl.szmd.szmd.feature_wm.R
import com.ctl.szmd.szmd.feature_wm.databinding.FragmentWmMainBinding
import com.ctl.szmd.szmd.feature_wm.di.wmModules
import com.ctl.szmd.szmd.feature_wm.ui.base.BaseWmBindingFragment
import kotlinx.android.synthetic.main.view_wm_main_bottom_layout.view.*
import org.koin.core.context.loadKoinModules


class WmMainFragment : BaseWmBindingFragment<FragmentWmMainBinding>() {

    private val loadModules by lazy {
        loadKoinModules(wmModules)
    }

    override fun getLayoutId(): Int = R.layout.fragment_wm_main

    override fun initView() {
        loadModules
        val token = LocalCache.getInstance().getString(BaseConstants.LOCAL_CACHE_MW_TOKEN)
        if (token.isNullOrBlank()) {
            findNavController().navigate(R.id.wmLoginFragment)
        }
        binding.apply {
            bindingView()
        }
        LiveBus.get(BaseConstants.LIVE_BUS_GOTO_WM_LOGIN).observe(viewLifecycleOwner, Observer {
            findNavController().navigate(R.id.wmLoginFragment)
        })
    }

    private val dataList: MutableList<Fragment> = mutableListOf()
    private fun FragmentWmMainBinding.bindingView() {
        dataList.clear()
        dataList.add(WmHomeFragment())
        dataList.add(MeFragment())
        dataList.add(MeFragment())
        dataList.add(MeFragment())
        dataList.add(MeFragment())
        viewPager2.offscreenPageLimit = dataList.size
        viewPager2.adapter = ViewPagerFragmentStateAdapter(this@WmMainFragment, dataList)
        viewPager2.isSaveEnabled = false

        val commonNavigator = CommonNavigator(requireContext())
        commonNavigator.setAdjustMode(true)
        commonNavigator.setAdapter(navigatorAdapter)
        indicator.setNavigator(commonNavigator)
        ViewPagerHelper.bind(indicator, viewPager2)

    }

    private val navigatorAdapter = object : CommonNavigatorAdapter() {
        override fun getCount(): Int {
            return dataList.size
        }

        @SuppressLint("InflateParams")
        override fun getTitleView(context: Context, index: Int): IPagerTitleView? {
            val commonPagerTitleView = CommonPagerTitleView(context)

            val customLayout: View =
                LayoutInflater.from(context).inflate(R.layout.view_wm_main_bottom_layout, null)
            val titleImg = customLayout.imageView
            val titleText = customLayout.textView
            when (index) {
                0 -> {
                    titleText.text = "首页"
                    titleImg.setImageResource(R.drawable.wm_icon_sy_normal)
                }
                1 -> {
                    titleText.text = "学习"
                    titleImg.setImageResource(R.drawable.wm_icon_xx_normal)
                }
                2 -> {
                    titleText.text = "随手拍"
                    titleImg.setImageResource(R.drawable.wm_icon_ssp_normal)
                }
                3 -> {
                    titleText.text = "互动"
                    titleImg.setImageResource(R.drawable.wm_icon_hd_normal)
                }
                4 -> {
                    titleText.text = "我的"
                    titleImg.setImageResource(R.drawable.wm_icon_gr_normal)
                }
            }
            commonPagerTitleView.setContentView(customLayout)
            commonPagerTitleView.setOnPagerTitleChangeListener(object :
                CommonPagerTitleView.OnPagerTitleChangeListener {
                override fun onSelected(index: Int, totalCount: Int) {
                    titleText.setTextColor(R.color.colorWmRed.getColor(requireContext()))
                    when (index) {
                        0 -> {
                            titleImg.setImageResource(R.drawable.wm_icon_sy_pressed)
                        }
                        1 -> {
                            titleImg.setImageResource(R.drawable.wm_icon_xx_pressed)
                        }
                        2 -> {
                            titleImg.setImageResource(R.drawable.wm_icon_ssp_pressed)
                        }
                        3 -> {
                            titleImg.setImageResource(R.drawable.wm_icon_hd_pressed)
                        }
                        4 -> {
                            titleImg.setImageResource(R.drawable.wm_icon_gr_pressed)
                        }
                    }
                }

                override fun onDeselected(index: Int, totalCount: Int) {
                    titleText.setTextColor(R.color.color6.getColor(requireContext()))
                    when (index) {
                        0 -> {
                            titleImg.setImageResource(R.drawable.wm_icon_sy_normal)
                        }
                        1 -> {
                            titleImg.setImageResource(R.drawable.wm_icon_xx_normal)
                        }
                        2 -> {
                            titleImg.setImageResource(R.drawable.wm_icon_ssp_normal)
                        }
                        3 -> {
                            titleImg.setImageResource(R.drawable.wm_icon_hd_normal)
                        }
                        4 -> {
                            titleImg.setImageResource(R.drawable.wm_icon_gr_normal)
                        }
                    }
                }

                override fun onLeave(
                    index: Int,
                    totalCount: Int,
                    leavePercent: Float,
                    leftToRight: Boolean
                ) {
//                        titleImg.scaleX = 1.3f + (0.8f - 1.3f) * leavePercent
//                        titleImg.scaleY = 1.3f + (0.8f - 1.3f) * leavePercent
                }

                override fun onEnter(
                    index: Int,
                    totalCount: Int,
                    enterPercent: Float,
                    leftToRight: Boolean
                ) {
//                        titleImg.scaleX = 0.8f + (1.3f - 0.8f) * enterPercent
//                        titleImg.scaleY = 0.8f + (1.3f - 0.8f) * enterPercent
                }
            })
            commonPagerTitleView.setOnClickListener {
//                val abs = Math.abs(binding.viewPager2.currentItem - index)
//                if (abs <= 1) {
//                    binding.viewPager2.currentItem = index
//                } else {
                    binding.viewPager2.setCurrentItem(index, false)
//                }
            }
            return commonPagerTitleView
        }

        override fun getIndicator(context: Context): IPagerIndicator? {
            return null
        }
    }

    override fun onStart() {
        super.onStart()
        binding.viewPager2.adapter?.notifyItemChanged(binding.viewPager2.currentItem)
    }
}